package club.xyes.zkh.ecommerce.service.impl;

import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.ShopMembership;
import club.xyes.zkh.ecommerce.commons.properties.WebsiteConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.util.DateTimeUtil;
import club.xyes.zkh.ecommerce.commons.util.MultipartFileUtil;
import club.xyes.zkh.ecommerce.dao.mapper.ShopMembershipMapper;
import club.xyes.zkh.ecommerce.service.ShopMembershipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Create by 郭文梁 2019/4/18 0018 14:41
 * ShopMembershipServiceImpl
 * 商铺会员定义相关业务行为实现
 *
 * @author 郭文梁
 * @data 2019/4/18 0018
 */
@Service
public class ShopMembershipServiceImpl extends BaseServiceImpl<ShopMembership> implements ShopMembershipService {
    /**
     * 默认会员卡图片
     */
    private static final String DEFAULT_MEMBERSHIP_IMAGE = "default.png";
    /**
     * 默认会员卡名称
     */
    private static final String DEFAULT_MEMBERSHIP_NAME = "会员折扣卡";
    private final ShopMembershipMapper mapper;
    private final WebsiteConfigurationProperties websiteConfigurationProperties;

    @Autowired
    public ShopMembershipServiceImpl(ShopMembershipMapper mapper,
                                     WebsiteConfigurationProperties websiteConfigurationProperties) {
        super(mapper);
        this.mapper = mapper;
        this.websiteConfigurationProperties = websiteConfigurationProperties;
    }

    @Override
    public List<ShopMembership> findByShop(Shop shop) {
        ShopMembership query = new ShopMembership();
        query.setShopId(shop.getId());
        return findByQuery(query);
    }

    @Override
    public boolean existsByShop(Shop shop) {
        ShopMembership query = new ShopMembership();
        query.setShopId(shop.getId());
        return count(query) > 0;
    }

    @Override
    public ShopMembership setImage(ShopMembership shopMembership, MultipartFile imageFile) throws IOException {
        String staticFilePath = websiteConfigurationProperties.getStaticFilePath();
        String shopMembershipImagePath = websiteConfigurationProperties.getShopMembershipImagePath();
        String path = staticFilePath + shopMembershipImagePath;
        String filename = MultipartFileUtil.save(imageFile, path);
        shopMembership.setImage(filename);
        return updateById(shopMembership);
    }

    @Override
    public ShopMembership getRequiredByIdFetchAll(Integer id) {
        return mapper.selectByIdFetchAll(id);
    }

    @Override
    public ShopMembership createDefault(Shop shop) {
        Calendar calendar = DateTimeUtil.calendar(DateTimeUtil.now());
        calendar.add(Calendar.YEAR, 10);
        Date expireTime = calendar.getTime();
        ShopMembership membership = new ShopMembership();
        membership.setShop(shop);
        membership.setShopId(shop.getId());
        membership.setImage(DEFAULT_MEMBERSHIP_IMAGE);
        membership.setEnabled(true);
        membership.setName(shop.getName());
        membership.setExpireTime(expireTime);
        return save(membership);
    }
}
